/**
 * 
 */
package com.lastproject.web.bean;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;
import com.lastproject.dto.DepartmentDTO;
import com.lastproject.dto.SearchUserDTO;
import com.lastproject.ejb.dao.DepartmentDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;

/**
 * @author Alexandra
 * 
 */
@ManagedBean
@RequestScoped
public class SearchUserTableData {

	@EJB
	DepartmentDAORemote depDAO;

	@EJB
	UserDAORemote userDAORemote;

	private DataModel<SearchUserDTO> filterModel = null;

	@ManagedProperty(value = "#{searchUserBean}")
	private SearchUserBean searchUserBean;

	private SearchUserDTO[] foundUserArr;

	@PostConstruct
	public void postConstr() {

		try {

			foundUserArr = new SearchUserDTO[searchUserBean
					.getSearchUserDTOList().length];

			for (int i = 0; i < searchUserBean.getSearchUserDTOList().length; i++) {
				foundUserArr[i] = searchUserBean.getSearchUserDTOList()[i];
			}
		} catch (NullPointerException e) {

		}

		for (SearchUserDTO user : foundUserArr) {
			System.out.println("USER: " + user.getId());

			if (user.getIdDepartment() != 0) {
				DepartmentDTO dep = depDAO.findById(user.getIdDepartment());
				user.setDepartmentName(dep.getName());
			} else {
				user.setDepartmentName("-");
			}
		}

		filterModel = new ArrayDataModel<SearchUserDTO>(foundUserArr);
		try {
			saveTempPhoto();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public SearchUserTableData() {

	}

	public DataModel<SearchUserDTO> getSurveysList() {
		return filterModel;
	}

	public SearchUserDTO[] getFoundUserArr() {
		return foundUserArr;
	}

	public void setFoundUserArr(SearchUserDTO[] foundUserArr) {
		this.foundUserArr = foundUserArr;
	}

	public SearchUserBean getSearchUserBean() {
		return searchUserBean;
	}

	public void setSearchUserBean(SearchUserBean searchUserBean) {
		this.searchUserBean = searchUserBean;
	}

	public String getTempPhotoById(final int id) throws IOException {
		String tmpfolder = FacesContext.getCurrentInstance().getExternalContext().getRealPath("resources/img");
		File dir = new File(tmpfolder);
		File[] matches = dir.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.contains("" + id + "photoTemp")
						&& name.endsWith(".jpg");
			}
		});
		
		//System.out.println(matches[0].toURI().toString().substring(6));
		FacesContext.getCurrentInstance().getExternalContext().getRealPath("WEB-INF");
		System.out.println(matches[0].getName());
		return matches[0].getName();
	}

	public void saveTempPhoto() throws IOException {
		for (final SearchUserDTO user : foundUserArr) {
			String tmpfolder = FacesContext.getCurrentInstance().getExternalContext().getRealPath("resources/img");
			File dir = new File(tmpfolder);

			File[] matches = dir.listFiles(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.contains("" + user.getId() + "photoTemp")
							&& name.endsWith(".jpg");
				}
			});
			boolean ok = true;

			for (File file : matches) {
				if (file.getName().contains("" + user.getId() + "photoTemp")) {
					ok = false;
				}
			}
			if (ok == true) {
				File iptmp = File.createTempFile("" + user.getId()
						+ "photoTemp", ".jpg", new File(tmpfolder));
				FileOutputStream os = new FileOutputStream(iptmp);
				os.write(user.getPhoto());
				os.close();
				iptmp.deleteOnExit();
			}
		}

	}

}
